Skip to content

Added square wave functionality to play tone. #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 9, 2022
Merged

Added square wave functionality to play tone. #116

merged 8 commits into from
May 9, 2022

Conversation

ryanskeith
Copy link
Contributor

Added code to enable square tone. This turns out to be much louder.
In this addition, the variable names that referred to sine waves were
generalized to _wave and _wave sample. This is a proposed solution
for issue #49. And the square wave is much louder.

Added code to enable square tone. This turns out to be much louder.
In this addition, the variable names that referred to sine waves were
generalized to _wave and _wave sample.
@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

The actions check failed due to code formatting. There is a guide page here that has more information and shows how to get set up locally to run the check and have it do the formatting changes for you. https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/check-your-code

We'll also need to double check whether there is enough room on the CPX builds to include this change. iirc it's a rather tight fit and this library is frozen in so increases in code size here can possibly make it not fit within that build.

@dhalbert dhalbert requested a review from kattni May 3, 2022 17:39
@ryanskeith
Copy link
Contributor Author

I feel this is completed and ready for review.

@kattni
Copy link
Contributor

kattni commented May 3, 2022

Unfortunately, I don't have a CPX with me to test this with. I can test the build sizes though.

@tekktrik tekktrik linked an issue May 3, 2022 that may be closed by this pull request
@tekktrik
Copy link
Member

tekktrik commented May 3, 2022

Linked the relevant issue since it seems to address it.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

I successfully tested the functionality with a custom build of CPX with this branch as the frozen in lib. It is a good margin louder.

I tried one of the builds that I knew from a prior PR was most limited in space and unfortunately this does push it over the limit:

make BOARD=circuitplayground_express_displayio TRANSLATION=ru
...
253564 bytes used, -124 bytes free in flash firmware space out of 253440 bytes (247.5kB).
12192 bytes used, 20576 bytes free in ram for stack and heap out of 32768 bytes (32.0kB).

Too little flash!!!

@kattni
Copy link
Contributor

kattni commented May 3, 2022

@FoamyGuy I completely forgot about the displayio build. I tested the regular build for CPX and it's fine.

@dhalbert Is there any way to shrink the displayio build? Or is that not feasible here?

@ryanskeith
Copy link
Contributor Author

ryanskeith commented May 3, 2022

So close, yet so far away... This issue may need to be dropped as I am not seeing a way to reduce how to simplify the solution further. Alternatively, you could adopt to use the square wave versus sine as it is louder.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

That thought of changing to use only the square wave function and remove sine crossed my mind as a possibility too. I don't necessarily have a super strong preference either way but the louder one is easier to tell when it's working depending on the frequencies used.

@kattni
Copy link
Contributor

kattni commented May 3, 2022

Let's also give Dan a chance to let us know if he can do something with the displayio build. If not, then we'll make a decision on how to move forward here.

@dhalbert
Copy link
Contributor

dhalbert commented May 3, 2022

Setting CIRCUITPY_ONEWIREIO = 0 in the build would recover 500-600 bytes.

@dhalbert
Copy link
Contributor

dhalbert commented May 3, 2022

So if you go ahead and submit this, then when we update the frozen modules the next time, I will also shrink that build.

@kattni
Copy link
Contributor

kattni commented May 3, 2022

Excellent, thank you @dhalbert!

@FoamyGuy Go ahead and review it with that in mind, thanks!

@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

I tried the same build with CIRCUITPY_ONEWIREIO = 0

But it still came out just a tad over:

253476 bytes used, -36 bytes free in flash firmware space out of 253440 bytes (247.5kB).
12192 bytes used, 20576 bytes free in ram for stack and heap out of 32768 bytes (32.0kB).

This is with the branch from this PR and CIRCUITPY_ONEWIREIO = 0 on CPX_Displayio with ru translation.

@kattni
Copy link
Contributor

kattni commented May 3, 2022

Oof, ok. @FoamyGuy let's still review this with the assumption that we can make it fit and then make a decision. Thanks!

@dhalbert
Copy link
Contributor

dhalbert commented May 3, 2022

Ugh, well, I think I can find something or other.

@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

fwiw I did also try with deleting bluefruit.py inside the frozen library and it does fit with a good amount of head-room like that:

252304 bytes used, 1136 bytes free in flash firmware space out of 253440 bytes (247.5kB).
12192 bytes used, 20576 bytes free in ram for stack and heap out of 32768 bytes (32.0kB).

(same build for CPX_Displayio ru and this branch of the library).

If we can come up with a mechanism that allows excluding files from there it could give us enough space I think.

Comment on lines 711 to 714
if waveform == "square":
self._wave = array.array("H", self._square_sample(length))
else:
self._wave = array.array("H", self._sine_sample(length))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may shrink things enough to fit

Suggested change
if waveform == "square":
self._wave = array.array("H", self._square_sample(length))
else:
self._wave = array.array("H", self._sine_sample(length))
self._wave = array.array("H", self._square_sample(length) if waveform == "square" else self._sine_sample(length))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be similar refactorings in the Python code that would save a few bytes here and there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if ternary operator worked in Circuit Python. I wrote it this way if anyone else wanted to add different shaped waves down the line. If it works, great but to me the sound difference isn't that big. Square definitely sounds more ragged than sine but that could be that the code is driving it rather hard.

for _ in range(half_length):
yield 0

def _generate_sample(self, length=100, waveform="sine"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waveform could take constant values instead of a string.

I don't know if strictly better one way or the other but I tend to try to use constant number values with descriptively named variables instead of strings when I write code like this.

I'm not sure my tendencies are worth holding this up over though so we can see if anyone else has thoughts on it.

i.e usage would look something like:

circuitplayground.start_tone(234, circuitplayground.SINE_WAVEFORM)

and the value can then be a constant int rather than a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great idea as well. Is there an enum type in circuit python?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we have enum, but we do have a const that is inherited from micropython: https://docs.circuitpython.org/en/latest/docs/library/micropython.html?#micropython.const

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the variable names may take some space as well it may be worthwhile to go with a more shorthand version like SINE_WAVE instead of SINE_WAVEFORM as well. Every bit counts to an extent here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good idea. I will look over variable names.

Copy link
Contributor

@dhalbert dhalbert May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SINE_WAVEFORM, etc. are going to be the equivalent of strings, so you will not gain space, unfortunately, I think. The names need to be available at run time.

@ryanskeith
Copy link
Contributor Author

ryanskeith commented May 3, 2022

I moved from string to number way of doing things to determine SINE or SQUARE. Let me know if it made the space cut @FoamyGuy, @kattni .

@FoamyGuy
Copy link
Contributor

FoamyGuy commented May 3, 2022

Tested that same cpx displayio+ru build on the latest version and still not quite fitting -56 bytes on the latest one.

We'll look into potential refactors to squeeze it a bit more and then eventually maybe try to have some mechanism that can exclude files that are unneeded to gain back some space.

@tekktrik
Copy link
Member

tekktrik commented May 4, 2022

Another potential fix is #117 by @Neradoc

@kattni
Copy link
Contributor

kattni commented May 9, 2022

Merging this with the understanding that we will need to tweak the build when we update the frozen modules.

@ryanskeith Thank you again!

@kattni kattni merged commit a6b5b88 into adafruit:main May 9, 2022
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 21, 2022
Updating https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground to 5.2.0 from 5.1.0:
  > Patch .pre-commit-config.yaml
  > Merge pull request adafruit/Adafruit_CircuitPython_CircuitPlayground#116 from ryanskeith/add_square_wave
  > change discord badge
  > Patch: Replaced discord badge image
  > Update .gitignore

Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 4.2.1 from 4.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#163 from dannystaple/patch-1
  > Patch .pre-commit-config.yaml
  > change discord badge
  > Patch: Replaced discord badge image
  > Update .gitignore
  > Update Black to latest.

Updating https://github.com/adafruit/Adafruit_CircuitPython_IL91874 to 1.2.4 from 1.2.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_IL91874#15 from makermelissa/master
  > Patch .pre-commit-config.yaml
  > change discord badge
  > Patch: Replaced discord badge image
  > Updated gitignore
  > Update Black to latest.
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_GC_IOT_Core to 3.2.0 from 3.1.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_GC_IOT_Core#23 from tannewt/use_real_ntp
  > Patch .pre-commit-config.yaml
  > change discord badge
  > Patch: Replaced discord badge image
  > Updated gitignore
  > Update Black to latest.

Updating https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT to 2.5.4 from 2.5.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#43 from tannewt/use_real_ntp
  > Merge pull request adafruit/Adafruit_CircuitPython_AzureIoT#45 from adafruit/delete-unused-license
  > Patch .pre-commit-config.yaml

Updating https://github.com/adafruit/Adafruit_CircuitPython_BLE_iBBQ to 1.2.8 from 1.2.7:
  > Merge pull request adafruit/Adafruit_CircuitPython_BLE_iBBQ#9 from sabadam32/add_type_annotations_6
  > Patch .pre-commit-config.yaml
  > change discord badge
  > Patch: Replaced discord badge image
  > Update .gitignore
  > Update Black to latest.
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32S2TFT to 1.1.0 from 1.0.1:
  > Update .pre-commit-config.yaml
  > Patch .pre-commit-config.yaml
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32S2TFT#6 from FoamyGuy/dont_require_secrets_if_no_network
  > change discord badge
  > Patch: Replaced discord badge image
  > Updated gitignore
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32S2TFT#5 from FoamyGuy/fix_circup_instructions

Updating https://github.com/adafruit/Adafruit_CircuitPython_OAuth2 to 1.0.7 from 1.0.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_OAuth2#7 from ktkinsey37/typing
  > Patch .pre-commit-config.yaml
  > change discord badge
  > Patch: Replaced discord badge image
  > Updated gitignore
  > Update Black to latest.
  > Fixed readthedocs build
  > Consolidate Documentation sections of README

Updating https://github.com/adafruit/Adafruit_CircuitPython_PIOASM to 0.7.3 from 0.7.2:
  > Patch .pre-commit-config.yaml
  > Merge pull request adafruit/Adafruit_CircuitPython_PIOASM#49 from jepler/undo-folder-structure
  > Merge pull request adafruit/Adafruit_CircuitPython_PIOASM#48 from jepler/7seg-counter

Updating https://github.com/adafruit/Adafruit_CircuitPython_Simple_Text_Display to 1.2.4 from 1.2.3:
  > Patch .pre-commit-config.yaml
  > Merge pull request adafruit/Adafruit_CircuitPython_Simple_Text_Display#12 from kattni/docs-fix
  > change discord badge
  > Patch: Replaced discord badge image
  > Updated gitignore
  > Update Black to latest.
  > Fixed readthedocs build
  > Post-patch cleanup
  > Consolidate Documentation sections of README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sine wave may be rather quiet - add option to change to square wave
5 participants